import { useState } from 'react';
import { useToasts } from '@geist-ui/react';
import fetch from '@mmstudio/an000058';
import { Message as M1, Result as R1 } from '../../api/account/login.api';
import api from '../../../atoms/api';

/**
 * 帐户登录
 */
export default function Account() {
	const [user, setuser] = useState('');
	const [psw, setpsw] = useState('');
	const [, toast] = useToasts();
	async function submit() {
		if (!user) {
			toast({
				text: '用户名不能为空',
				type: 'error'
			});
			return;
		}
		if (!psw) {
			toast({
				text: '密码不能为空',
				type: 'error'
			});
		}
		try {
			const ret = await fetch<R1, M1>(api['/api/account/login'], 'post', { user, psw });
			if (ret) {
				toast({
					text: '登陆成功',
					type: 'success'
				});
				window.location.assign(api['/api/account/go']);
			} else {
				toast({
					text: '登陆失败',
					type: 'error'
				});
			}
		} catch (error) {
			toast({
				text: `登陆失败:${(error as Error).message}`,
				type: 'error'
			});
		}
	}
	return <div className="content">
		<div className="linecontainer">
			<i className="label"></i>
			<input type="text" placeholder="用户名" className="input" defaultValue={user} onChange={(e) => {
				setuser(e.target.value);
			}} onKeyPress={(e) => {
				if (e.nativeEvent.key === 'Enter') {
					submit();
				}
			}} />
		</div>
		<div className="linecontainer">
			<i className="label"></i>
			<input type="password" placeholder="密码" className="input" onChange={(e) => {
				setpsw(e.target.value);
			}} onKeyPress={(e) => {
				if (e.nativeEvent.key === 'Enter') {
					submit();
				}
			}} />
		</div>
		<input type="button" value="登录" className="loginbtn" onClick={submit} />
		<style jsx>{`
.content {
	padding:0;
	display: flex;
	flex-direction: column;
	justify-content: center;
}
.linecontainer {
	width: 20rem;
	border-radius: 4px;
	border: 1px solid #ddd;
	display: flex;
	justify-content: center;
	line-height: 5rem;
	margin-bottom: 2rem;
}
.label {
	color:#999;
	font-size: 22px;
	left: 15px
}
.input {
	width: 100%;
	height: 2.5rem;
	line-height: 2.5rem;
	padding-left: 3.8rem;
	font-size: 1rem;
	vertical-align: top;
	-webkit-appearance: none;
	background-color: #FFF;
	border-radius: 4px;
	border: 1px solid #DCDFE6;
	-webkit-box-sizing: border-box;
	box-sizing: border-box;
	color: #606266;
	display: inline-block;
	outline: 0;
	padding: 0 15px;
	-webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
	transition: border-color .2s cubic-bezier(.645,.045,.355,1);
}
.input:hover {
	border-color: #C0C4CC;
}
.input:focus {
	border-color: var(--color-primary);
}
.loginbtn {
	border-radius: 2rem;
	line-height: 2.5rem;
	border-style: none;
	text-align: center;
	cursor: pointer;
	font-size: 1rem;
	color: var(--color-whites);
	background: var(--color-primary);
	border-color: var(--color-primary);
	transition: all 600ms;
}
.loginbtn:hover{
	background: var(--color-primary-light-3);
	border-color: var(--color-primary-light-3);
}
.loginbtn:active{
	background: var(--color-primary);
	border-color: var(--color-primary);
}
`}</style>
	</div>;
}
